package com.qingcheng.controller;

import com.qingcheng.pojo.goods.TbCategoryBrand;
import com.qingcheng.service.TbCategoryBrandService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
*
* @author Simon
* @date 2020/07/27
*/
@Slf4j
@RestController
@RequestMapping("/categoryBrand")
@Api(tags = "TbCategoryBrandController")
public class TbCategoryBrandController {

    @Resource
    private TbCategoryBrandService tbCategoryBrandService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbCategoryBrand tbCategoryBrand = tbCategoryBrandService.findById(id);
        return YouthR.ok().data("tbCategoryBrand",tbCategoryBrand);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbCategoryBrand>  list = tbCategoryBrandService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbCategoryBrand> pageUtils = tbCategoryBrandService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,
                                    @RequestBody TbCategoryBrand tbCategoryBrand){
        PageUtil<TbCategoryBrand> pageUtils = tbCategoryBrandService.pageQueryByAll(pageNum, pageSize, tbCategoryBrand);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public YouthR add(@RequestBody TbCategoryBrand tbCategoryBrand) {

        boolean b = tbCategoryBrandService.saveTbCategoryBrand(tbCategoryBrand);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbCategoryBrand tbCategoryBrand) {
        boolean b = tbCategoryBrandService.updateTbCategoryBrand(tbCategoryBrand);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbCategoryBrandService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }
}
